package net.oschina.aibinxiao.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.oschina.aibinxiao.domain.User;
import net.oschina.aibinxiao.service.UserService;
import net.oschina.aibinxiao.service.impl.UserServiceImpl;

/**
 * Servlet implementation class ActiveServlet
 */
@WebServlet("/ActiveServlet")
public class ActiveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 接收激活码
		String code = request.getParameter("code");
		String email = request.getParameter("email");
		// 根据email查询用户
		UserService userService = new UserServiceImpl();
		
		try {
			User user = userService.findByEmail(email);
			userService.validateActiveCode(user, code);
			request.setAttribute("msg", "您已经激活成功！请去登录！");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("msg", e.getMessage());
		}
		// 页面跳转
		request.getRequestDispatcher("/msg.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
